import { _decorator, Component, instantiate, Node, Prefab } from "cc";
import { GameWeapon } from "./GameWeapon";
import { Gun } from "./Gun";
import PlayerController from "../PlayerController";
import { EventManager, MyEvent } from "../AshFramework/Managers/EventManager";
import { ResourceUtil } from "../AshFramework/Utils/ResourceUtil";
import { GameManager } from "./GameManager";
import { HintController } from "./HintController";
import { BundleManager, Bundles } from "../AshFramework/Managers/BundleManager";
import { BulletController } from "./BulletController";
import { TLWLSJ_UIManager } from "./UIManager";
import { Magazine } from "./Magazine";
import { CAPACITY, UITYPE, WEAPON, WEAPONBULLET } from "../AshFramework/Datas/Constant";
import { ZTool } from "../AshFramework/Utils/ZTool";
import { PrefsManager } from "../AshFramework/Managers/PrefsManager";
import { GameData } from "./GameData";
import { AudioManager, Audios } from "../AshFramework/Managers/AudioManager";
const { ccclass } = _decorator;

enum Ani {
  None = "",
  Fire = "kaiqiang",
  Reload = "huandan",
}

@ccclass("GameWeapon_ZDSQ")
export class GameWeapon_ZDSQ extends GameWeapon {
  Muzzle: Node = null;
  private _data: Gun = null;

  protected start(): void {
    this.Muzzle = this.node.getChildByName("枪口");
    this._data = this.Data as Gun;
    PlayerController.Instance.WeaponTs = this;
    this.scheduleOnce(() => {
      this.updateWeaponShow(this._data.CurMagazine);
    }, 0.1);
  }

  //却换弹夹
  switchMagazine() {
    if (TLWLSJ_UIManager.Instance.IsLock) {
      this.getWeapon();
      return;
    }
    if (this._data.HaveMagazine.length <= 0) {
      TLWLSJ_UIManager.Instance.showDXPanel(UITYPE.背包, () => {
        TLWLSJ_UIManager.Instance.closeDXPanel();
        this.IsReload = true;
        AudioManager.PlaySound(Audios.Reload);
        this.playAni(Ani.Reload, false, () => {
          this.State = Ani.None;
          this.IsReload = false;
          if (this._data.CurMagazine && this._data.CurMagazine.Bullets.length > 0) {
            this._data.HaveMagazine.push(this._data.CurMagazine);
          }
          const weaponName = ZTool.GetEnumKeyByValue(WEAPON, this.WeaponType);
          this._data.CurMagazine = new Magazine(CAPACITY[weaponName], WEAPONBULLET[weaponName]);
          this.updateWeaponShow(this._data.CurMagazine);
        });
      })
      return;
    }
    this.IsReload = true;
    if (this._data.CurMagazine && this._data.CurMagazine.Bullets.length > 0) {
      AudioManager.PlaySound(Audios.Reload);
      this.playAni(Ani.Reload, false, () => {
        this.State = Ani.None;
        this.IsReload = false;
        this._data.HaveMagazine.push(this._data.CurMagazine);
        this._data.CurMagazine = this._data.HaveMagazine.shift();
        this.updateWeaponShow(this._data.CurMagazine);
        this.IsStop ? this.endAttack() : this.attack();
      });
    } else {
      AudioManager.PlaySound(Audios.Reload);
      this.playAni(Ani.Reload, false, () => {
        this.State = Ani.None;
        this.IsReload = false;
        this._data.CurMagazine = this._data.HaveMagazine.shift();
        this.updateWeaponShow(this._data.CurMagazine);
        this.IsStop ? this.endAttack() : this.attack();
      });
    }
  }

  FL() {
    const weaponName = ZTool.GetEnumKeyByValue(WEAPON, this.WeaponType);
    this._data.CurMagazine = new Magazine(CAPACITY[weaponName], WEAPONBULLET[weaponName]);
    this.updateWeaponShow(this._data.CurMagazine);
  }

  attack() {
    if (TLWLSJ_UIManager.Instance.IsLock) {
      this.getWeapon();
      return;
    }
    if (!this.isAttack()) return;
    if (this.IsStop) {
      this.playAni(Ani.None, false);
      return;
    }
    if (this._data.CurMagazine && this._data.CurMagazine.Bullets.length > 0) {
      const bullet = this._data.CurMagazine.Bullets.shift();
      this.IsFire = true;
      AudioManager.PlaySound(Audios.Fire);
      this.playAni(Ani.Fire, false, () => {
        this.IsFire = false;
        this.State = Ani.None;
        this.fireBullet(bullet);
        this.updateWeaponShow(this._data.CurMagazine);
        this.attack();
        //发起攻击
      });
    } else {
      this.switchMagazine();
    }
  }

  //发射子弹
  fireBullet(bulletName: string) {
    BundleManager.LoadPrefab(Bundles.Prefabs, "子弹").then((prefab: Prefab) => {
      const bullet: Node = instantiate(prefab);
      bullet.parent = GameManager.Instance.BulletLayout;
      bullet.setWorldPosition(this.Muzzle.getWorldPosition());
      bullet.getComponent(BulletController).fire(bulletName, PlayerController.Instance.DirX, PlayerController.Instance.DirY);
    });
  }

  protected onEnable(): void {
    super.onEnable();
    EventManager.on(MyEvent.RELOAD, this.switchMagazine, this);
    EventManager.on(MyEvent.FL, this.FL, this);
  }

  protected onDisable(): void {
    super.onDisable();
    EventManager.off(MyEvent.RELOAD, this.switchMagazine, this);
    EventManager.off(MyEvent.FL, this.FL, this);
  }
}
